package com.robot.config;


import com.robot.dao.handler.JacksonObjectMapper;
import com.robot.interceptors.IntegralAccountInterceptor;
import com.robot.interceptors.JwtInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    private final HttpMessageConverters messageConverters;
    public WebMvcConfig(HttpMessageConverters messageConverters) {
        this.messageConverters = messageConverters;
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter messageconverter = new MappingJackson2HttpMessageConverter();
        messageconverter.setObjectMapper(new JacksonObjectMapper());
        converters.add(0,messageconverter);
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(taskExecutor());
        configurer.setDefaultTimeout(30000); // 设置超时时间，单位为毫秒
    }

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(50);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("async-task-");
        executor.initialize();
        return executor;
    }

    @Bean(name = "excelProcessingTaskExecutor")
    public ThreadPoolTaskExecutor excelProcessingTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(50);
        executor.setThreadNamePrefix("excel-task-");
        executor.initialize();
        return executor;
    }

    @Autowired
    private IntegralAccountInterceptor integralAccountInterceptor;

    @Autowired
    private JwtInterceptor jwtInterceptor;


    private ExecutorService executorService = null;

    /**
     * @param registry 注册表
     * @Description 通过 registry 来设置允许跨域的路由、
     * 请求方式、允许顺序等消息头，并在 Spring 容器中创建注册器
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路由
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }


    /**
     * @param registry 注册表
     * @Description 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();
        //排除拦截，除了注册登录其他都拦截
        excludePath.add("/login/*");
        //放行 swagger
        excludePath.add("/swagger-resources/**");
        excludePath.add("/webjars/**");
        excludePath.add("/v2/**");
        excludePath.add("/swagger-ui.html/**");

        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);

        registry.addInterceptor(integralAccountInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);

        WebMvcConfigurer.super.addInterceptors(registry);
    }





}
